package com.hspedu.list_;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * @Author Stringzhua
 * @Date 2024/3/11 18:14
 * description:
 */
@SuppressWarnings("all")
public class LinkedListCRUD {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        System.out.println("linkedList = " + linkedList);

        //演示删除节点的方法
//        linkedList.remove();//这里默认删除的是第一个节点
////        linkedList.remove(2);
//        System.out.println("linkedList = " + linkedList);

        //修改某个节点对象
        linkedList.set(1,999);
        System.out.println("linkedList = " + linkedList);

        //得到某个节点对象
        //get(1)是得到双向链表的第二个对象
        Object o = linkedList.get(1);
        System.out.println("o = " + o);//999

        //因为LinkedList是实现了List接口,所以它的遍历方式可以是增强for循环|迭代器
        System.out.println("===迭代器遍历===");
        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println("next = " + next);
        }
        System.out.println("===增强for循环遍历===");
        for (Object o1 :linkedList) {
            System.out.println("o1 = " + o1);
        }
        System.out.println("===普通for循环遍历===");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        /*
        * 源码阅读
        * 1.LinkedList linkedList = new LinkedList();
        *     public LinkedList() {
             }
        * 2.这时 linkedList 的属性first=null last=null
        * 3.执行 添加
        *  public boolean add(E e) {
            linkLast(e);
            return true;
            }
        *4.将新的节点加入到双向链表的最后
        * void linkLast(E e) {
            final Node<E> l = last;
            final Node<E> newNode = new Node<>(l, e, null);
            last = newNode;
            if (l == null)
                first = newNode;
            else
                l.next = newNode;
            size++;
            modCount++;
        }
        * */


        /*
        * 源码解读:读删除的源码
        * 1.linkedList.remove();//这里默认的是删除第一个节点
        * 执行:removeFirst
        *   public E remove() {
            return removeFirst();
            }
        * 2.执行unlinkFirst
        *  public E removeFirst() {
            final Node<E> f = first;
            if (f == null)
            throw new NoSuchElementException();
            return unlinkFirst(f);
            }
        * 3.执行unlinkFirst,将f指向的双向链表的第一个节点拿掉[删除]
        *  private E unlinkFirst(Node<E> f) {
            // assert f == first && f != null;
            final E element = f.item;
            final Node<E> next = f.next;
            f.item = null;
            f.next = null; // help GC
            first = next;
            if (next == null)
                last = null;
            else
                next.prev = null;
            size--;
            modCount++;
            return element;
            }
        * */
    }
}
